package edu.nwpu.controller;


import com.alibaba.fastjson.JSON;
import edu.nwpu.pojo.Food;
import edu.nwpu.service.FoodService;
import edu.nwpu.service.FoodTypeService;
import edu.nwpu.utils.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

@RestController
public class FoodController {

    /**
     * 默认查询数量
     */
    private final Integer DEFAULT_LIMIT = 10;

    @Resource
    private FoodService foodService;

    @ApiOperation(value = "分页获取菜品数据", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping({"/food/list", "/food/list/{offset}", "/food/list/{offset}/{limit}"})
    public String listFoods(@PathVariable(name = "offset", required = false) Integer offset,
                            @PathVariable(name = "limit", required = false) Integer limit) {
        limit = (limit == null || limit < 1) ?  DEFAULT_LIMIT : limit;
        offset = (offset == null) ? 0 : offset;
        return JSON.toJSONString(ResponseUtil.responseQueryList(foodService.getFoodsPage(offset, limit)));
    }

    @ApiOperation(value = "根据菜品id查询菜品数据", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/food/query/id/{foodId}")
    public String queryFoodById(@PathVariable("foodId") Long id) {
        Food food = foodService.getFoodById(id);
        return JSON.toJSONString(ResponseUtil.responseQueryOne(food));
    }

    @GetMapping("/food/query/name/{foodName}")
    public String queryFoodsByName(@PathVariable("foodName") String name) {
        List<Food> foods = foodService.getFoodsByLikeName(name);
        return JSON.toJSONString(ResponseUtil.responseQueryList(foods));
    }

    @ApiOperation(value = "根据菜品类型id查询同一个类型的所有菜品", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/food/query/type/typeId/{typeId}")
    public String queryFoodByTypeId(@PathVariable("typeId") Long typeId) {
        List<Food> foods = foodService.getFoodsByTypeId(typeId);
        return JSON.toJSONString(ResponseUtil.responseQueryList(foods));
    }

    @PostMapping("/manage/food/update")
    @Deprecated
    public String updateFood(Food food) {
        boolean status = foodService.updateFood(food);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @PostMapping("/manage/food/add")
    @Deprecated
    public String addFood(Food food) {
        boolean status = foodService.addFood(food);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @GetMapping("/manage/food/delete/id/{id}")
    @Deprecated
    public String deleteFoodById(@PathVariable("id") Long id) {
        boolean status = foodService.deleteFoodById(id);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

}
